﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MyLib;
 

namespace PerformanceReport
{
    public partial class ProcessSteps : System.Web.UI.Page
    {
        protected DataTable dt = new DataTable();
        protected DataTable dtTemp = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindProcess();
                //隐藏图形化报表            
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true);
            }
        }

        //绑定下拉框
        public void DataBindProcess()
        {
            DataTable dtProcess;
            string strSql = "select Distinct ProcessName from PROCESSES ";
            dtProcess = DataAccess.Instance("UltDB").ExecuteDataTable(strSql.ToString());
            drpProcessName.DataSource = dtProcess;

            drpProcessName.DataTextField = "ProcessName";

            drpProcessName.DataValueField = "ProcessName";

            drpProcessName.DataBind();

            drpProcessName.Items.Insert(0, new ListItem("--请选择--", "0"));

        }


        private void BindData()
        {
            try
            {
                string strSql = "select Distinct Steplabel,ProcessName from Tasks ";                
                string where = " where 1=1 ";               
                where += "and ProcessName= '" + drpProcessName.SelectedValue.Trim() + "'";
                if (!string.IsNullOrEmpty(txtStart.Text))
                {
                    where += " and  starttime >= '" + txtStart.Text.Trim() + "'";
                }
                if (!string.IsNullOrEmpty(txtEnd.Text))
                {
                    where += " and  starttime <= '" + txtEnd.Text.Trim() + "'";
                }

                string StrSqlTotal = strSql + where;
                dt = DataAccess.Instance("UltDB").ExecuteDataTable(StrSqlTotal.ToString());

                if (dt.Rows.Count > 0)
                {
                    DataTable dtTemp = new DataTable();
                    dtTemp.Columns.Add("Steplabel");
                    dtTemp.Columns.Add("ProcessName");
                    DataRow NewRow = dtTemp.NewRow();
                    NewRow["Steplabel"] = dt.Rows[0]["Steplabel"].ToString();
                    NewRow["ProcessName"] = dt.Rows[0]["ProcessName"].ToString();
                    dtTemp.Rows.Add(NewRow);
                    //设置Repeater的数据源为分页数据源
                    rpSource.DataSource = dtTemp;
                    rpSource.DataBind();
                }
                else
                {
                    //Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('divList').style.display='none'", true); 
                }
            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        //流程审批步骤
        public string InitLine()
        {
            string strHtml = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strHtml += "<td>" + dt.Rows[i]["Steplabel"] + "</td>";
            }
            return strHtml;
        }

        //平均耗时
        public string TotalProcessStepsDosage()
        {
            string strHtmlCount = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strSql = "select  count(incident) cou,Steplabel,ProcessName, "                             
                             // + "Round(SUM((endtime - starttime) * 24),2) as times"
                             + "SUM(datediff(day,endtime, starttime) * 24) as times "
                              + " from Tasks"
                              + " where ProcessName= '" + drpProcessName.SelectedValue.Trim() + "'"
                              + " and Steplabel='" + dt.Rows[i]["Steplabel"].ToString() + "'"
                              + "  and status=3 group by Steplabel,ProcessName";
                DataTable dtCount = DataAccess.Instance("UltDB").ExecuteDataTable(strSql.ToString());
                if (dtCount.Rows.Count > 0)
                {
                    //四舍五入,保留一位有效数字                   
                    double time = double.Parse(dtCount.Rows[0]["times"].ToString());
                    double Line = double.Parse(dtCount.Rows[0]["cou"].ToString());                   
                    double strAvgTime = Math.Round((time / Line), 1, MidpointRounding.AwayFromZero);
                    strHtmlCount += "<td>" + strAvgTime + "</td>";
                }
                else
                {
                    strHtmlCount += "<td>" + 0 + "</td>";
                }
            }

            return strHtmlCount;
        }    

        private void MessageBox(string script)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "javascript", "<script language='javascript' defer>" + script + "</script>");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true);

        }

        public string getTime(string second)
        {
            string strRtn = "";
            if (!String.IsNullOrEmpty(second))
            {
                int sec = int.Parse(second);
                if (sec < 0)
                {
                    strRtn = "0";
                }
                else
                {
                    int day = sec / (60 * 60 * 24);
                    int hours = (sec % (60 * 60 * 24)) / (60 * 60);
                    int mins = ((sec % (60 * 60 * 24)) % (60 * 60)) / 60 + 1;

                    strRtn = day.ToString() + "天" + hours.ToString() + "小时" + mins.ToString() + "分钟";
                }
            }
            else
            {
                strRtn = "无";
            }
            return strRtn;
        }
    }
}